using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services.Trans.C2b;
using JlPay.Sdk.Models.Trans.C2b;
using JlPay.Sdk.Models.Trans.Dto;

namespace JlPay.Sdk.Examples.Trans.C2b
{
    /// <summary>
    /// 交易 - C2B二维码支付预授权功能使用示例
    /// </summary>
    public class QrcodePayPreAuthDemo
    {
        /// <summary>
        /// 构建二维码支付预授权请求
        /// </summary>
        /// <returns>请求对象</returns>
        private static QrcodePayPreAuthRequest BuildQrcodePayPreAuthRequest()
        {
            var request = new QrcodePayPreAuthRequest();
            
            // 设置请求参数
            request.MchId = "849584358120018";
            request.TermNo = "60131234";
            request.PayType = "wxpay";
            request.OutTradeNo = "ld201911023465529";
            request.PaymentValidTime = "10";
            request.Body = "充值中心-会员充值";
            request.Attach = "商城—-易拉宝10个打印纸20箱";
            request.TotalFee = "100";
            request.Remark = "该订单物流请走顺丰快递";
            request.NotifyUrl = "https://example.com/notify";
            request.OpUserId = "001";
            request.OpShopId = "GD_SZ_001";
            request.DeviceInfo = "N50007A8890";
            request.MchCreateIp = "123.12.12.123";
            request.TransAddress = "深圳市南山区科技二路10号";
            request.AreaCode = "4403";
            request.Longitude = "116.078631";
            request.Latitude = "39.71274";
            request.SubAppid = "wxid_129752pfasf1671";
            
            // 设置终端信息
            var terminalInfo = new TerminalInfoDto();
            terminalInfo.NetworkLicense = "123456789";
            terminalInfo.DeviceType = "01";
            terminalInfo.SerialNum = "TERM001";
            request.TerminalInfo = terminalInfo;
            
            return request;
        }

        /// <summary>
        /// 二维码支付预授权示例
        /// </summary>
        public static async Task QrcodePayPreAuthDemoAsync()
        {
            Console.WriteLine("开始执行二维码支付预授权示例");

            // 使用DemoConfig创建配置对象
            var config = DemoConfig.CreateConfigWithCrypto();
            Console.WriteLine("配置初始化完成，已启用自动加密和解密功能");

            var qrcodePayPreAuthService = new QrcodePayPreAuthService(config);

            // 创建二维码支付预授权请求
            var request = BuildQrcodePayPreAuthRequest();

            try
            {
                // 发送请求
                var response = await qrcodePayPreAuthService.QrcodePayPreAuthAsync(request);

                // 使用优化的响应处理
                response.HandleResponse("二维码支付预授权", config);
            }
            catch (Exception ex)
            {
                // 使用优化的异常处理
                var response = new QrcodePayPreAuthResponse();
                response.HandleResponseWithException("二维码支付预授权", config, ex);
            }

            Console.WriteLine("二维码支付预授权示例执行完毕");
        }

        /// <summary>
        /// 主程序入口
        /// </summary>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 二维码支付预授权示例 开始 =====");
            Console.WriteLine("=" + new string('=', 50));

            await QrcodePayPreAuthDemoAsync();

            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 二维码支付预授权示例执行完毕 =====");
            Console.WriteLine("=" + new string('=', 50));
        }
    }
} 